home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Source ƒ / egrep-1.5 / grep-src / alloca.c next >
Encoding:
C/C++ Source or Header  |  1991-10-20  |  3.9 KB  |  141 lines

  1. /*  This file has been changed-- it is not the standard FSF version.
  2.  
  3.                          Oct 1991.  */
  4.  
  5. /*
  6.     alloca -- (mostly) portable public-domain implementation -- D A Gwyn
  7.  
  8.     last edit:    86/05/30    rms
  9.        include config.h, since on VMS it renames some symbols.
  10.        Use xmalloc instead of malloc.
  11.  
  12.     This implementation of the PWB library alloca() function,
  13.     which is used to allocate space off the run-time stack so
  14.     that it is automatically reclaimed upon procedure exit, 
  15.     was inspired by discussions with J. Q. Johnson of Cornell.
  16.  
  17.     It should work under any C implementation that uses an
  18.     actual procedure stack (as opposed to a linked list of
  19.     frames).  There are some preprocessor constants that can
  20.     be defined when compiling for your specific system, for
  21.     improved efficiency; however, the defaults should be okay.
  22.  
  23.     The general concept of this implementation is to keep
  24.     track of all alloca()-allocated blocks, and reclaim any
  25.     that are found to be deeper in the stack than the current
  26.     invocation.  This heuristic does not reclaim storage as
  27.     soon as it becomes invalid, but it will do so eventually.
  28.  
  29.     As a special case, alloca(0) reclaims storage without
  30.     allocating any.  It is a good idea to use alloca(0) in
  31.     your main control loop, etc. to force garbage collection.
  32. */
  33.  
  34. #define    NULL    0            /* null pointer constant */
  35. #define pointer void*
  36.  
  37. extern void    free();
  38. extern pointer xmalloc();
  39.  
  40. /*
  41.     Define STACK_DIRECTION if you know the direction of stack
  42.     growth for your system.
  43.  
  44.     STACK_DIRECTION > 0 => grows toward higher addresses
  45.     STACK_DIRECTION < 0 => grows toward lower addresses
  46. */
  47.  
  48. #define STACK_DIRECTION -1
  49.  
  50. #ifndef STACK_DIRECTION
  51. #define    STACK_DIRECTION    0        /* direction unknown */
  52. #endif
  53.  
  54. #define    STACK_DIR    STACK_DIRECTION    /* known at compile-time */
  55.  
  56. /*
  57.     An "alloca header" is used to:
  58.     (a) chain together all alloca()ed blocks;
  59.     (b) keep track of stack depth.
  60.  
  61.     It is very important that sizeof(header) agree with malloc()
  62.     alignment chunk size.  The following default should work okay.
  63. */
  64.  
  65. #define ALIGN_SIZE sizeof(long int)
  66.  
  67. #ifndef    ALIGN_SIZE
  68. #define    ALIGN_SIZE    sizeof(double)
  69. #endif
  70.  
  71. typedef union hdr
  72. {
  73.   char    align[ALIGN_SIZE];    /* to force sizeof(header) */
  74.   struct
  75.     {
  76.       union hdr *next;        /* for chaining headers */
  77.       char *deep;        /* for stack depth measure */
  78.     } h;
  79. } header;
  80.  
  81. /*
  82.     alloca( size ) returns a pointer to at least `size' bytes of
  83.     storage which will be automatically reclaimed upon exit from
  84.     the procedure that called alloca().  Originally, this space
  85.     was supposed to be taken from the current stack frame of the
  86.     caller, but that method cannot be made to work for some
  87.     implementations of C, for example under Gould's UTX/32.
  88. */
  89.  
  90. static header *last_alloca_header = NULL; /* -> last alloca header */
  91.  
  92. pointer
  93. alloca (size)            /* returns pointer to storage */
  94.      unsigned    size;        /* # bytes to allocate */
  95. {
  96.   auto char    probe;        /* probes stack depth: */
  97.   register char    *depth = &probe;
  98.  
  99.                 /* Reclaim garbage, defined as all alloca()ed storage that
  100.                    was allocated from deeper in the stack than currently. */
  101.  
  102.   {
  103.     register header    *hp;    /* traverses linked list */
  104.  
  105.     for (hp = last_alloca_header; hp != NULL;)
  106.       if (STACK_DIR > 0 && hp->h.deep > depth
  107.       || STACK_DIR < 0 && hp->h.deep < depth)
  108.     {
  109.       register header    *np = hp->h.next;
  110.  
  111.       free ((pointer) hp);    /* collect garbage */
  112.  
  113.       hp = np;        /* -> next header */
  114.     }
  115.       else
  116.     break;            /* rest are not deeper */
  117.  
  118.     last_alloca_header = hp;    /* -> last valid storage */
  119.   }
  120.  
  121.   if (size == 0)
  122.     return NULL;        /* no allocation required */
  123.  
  124.   /* Allocate combined header + user data storage. */
  125.  
  126.   {
  127.     register pointer    new = xmalloc (sizeof (header) + size);
  128.     /* address of header */
  129.  
  130.     ((header *)new)->h.next = last_alloca_header;
  131.     ((header *)new)->h.deep = depth;
  132.  
  133.     last_alloca_header = (header *)new;
  134.  
  135.     /* User storage begins just after header. */
  136.  
  137.     return (pointer)((char *)new + sizeof(header));
  138.   }
  139. }
  140.  
  141.